#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h> 

int callback(void *NotUsed, int argc, char **argv, char **azColName){
   int i;
   for(i=0; i<argc; i++){
      printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
   }
   printf("\n");
   return 0;
}

int main(int argc, char* argv[])
{
    int  rc;
   /* Open database */
   sqlite3 *db=  nullptr;
   rc = sqlite3_open("test.db", &db);
   if( rc ){
      fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
      exit(0);
   }else{
      fprintf(stdout, "Opened database successfully\n");
   }
      const char *sql;
   /* Create SQL statement */
//    sql = "CREATE TABLE COMPANY("  \
//          "ID INT PRIMARY KEY     NOT NULL," \
//          "NAME           TEXT    NOT NULL," \
//          "AGE            INT     NOT NULL," \
//          "ADDRESS        CHAR(50)," \
//          "SALARY         REAL );";

//    /* Execute SQL statement */
   char *zErrMsg = 0;
//    rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
//    if( rc != SQLITE_OK ){
//    fprintf(stderr, "SQL error: %s\n", zErrMsg);
//       sqlite3_free(zErrMsg);
//    }else{
//       fprintf(stdout, "Table created successfully\n");
//    }

//插入数据
   /* Create SQL statement */
//    sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "  \
//          "VALUES (1, 'Paul', 32, 'California', 20000.00 ); " \
//          "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "  \
//          "VALUES (2, 'Allen', 25, 'Texas', 15000.00 ); "     \
//          "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)" \
//          "VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );" \
//          "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)" \
//          "VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );";

//    /* Execute SQL statement */
//    rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
//    if( rc != SQLITE_OK ){
//       fprintf(stderr, "SQL error: %s\n", zErrMsg);
//       sqlite3_free(zErrMsg);
//    }else{
//       fprintf(stdout, "Records created successfully\n");
//    }

   /* Create SQL statement */
   const char* data = "Callback function called";
   sql = "SELECT * from COMPANY";

   /* Execute SQL statement */
   rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);
   if( rc != SQLITE_OK ){
      fprintf(stderr, "SQL error: %s\n", zErrMsg);
      sqlite3_free(zErrMsg);
   }else{
      fprintf(stdout, "Operation done successfully\n");
   }

   sqlite3_close(db);
   
   return 0;
}
